package linkQuestion

// 反转链表  https://leetcode-cn.com/problems/reverse-linked-list/

type ListNode struct {
	Next *ListNode
	Val  int
}

func ReverseList(head *ListNode) *ListNode {
	var pre, next *ListNode
	for head != nil {
		next = head.Next
		head.Next = pre
		pre = head
		head = next
	}

	return pre
}

// 删除单链表内指定的值 https://leetcode-cn.com/problems/remove-linked-list-elements/
func RemoveValue(head *ListNode, val int) *ListNode {
	for head != nil {
		if head.Val != val {
			break
		}
		head = head.Next
	}

	cur, pre := head, head
	for cur != nil {
		if cur.Val == val {
			pre.Next = cur.Next
		} else {
			pre = cur
		}
		cur = cur.Next
	}

	return head
}
